//
/**
*
*File name:		LXAlertBaseViewController.swift
*History:		yoctech create on 2021/8/3       
*Description:
	
*/


import UIKit

open class LXAlertBaseViewController: UIViewController, LXAlertProtocol {
    open var alertLevel: Int = 0

    open var alertIdentifier: String?
    open var alertAnimationDuration: TimeInterval = 0.3

    open weak var parentVC: UIViewController?

    open lazy var showView:UIView = {
        let value = UIView.init()

        return value
    }()
    
    open lazy var btn : UIButton = {
        let value = UIButton.init()
        value.addTarget(self, action: #selector(btnClicked), for: .touchUpInside)
        return value
    }()
    
    open lazy var dragGesture: UIPanGestureRecognizer = {
        let ges = UIPanGestureRecognizer()
        ges.addTarget(self, action: #selector(onDrag))
        return ges
    }()

    public var animated: Bool = true

    ///是否可点击黑色背景消失
    open var backgroundHideEnabled : Bool =  true
    /// 是否可以拖动内容区域消失
    open var showViewDragHideEnabled : Bool =  true

    open var sureBlock : (()-> Void)?
    open var cancelBlock : (()-> Void)?
    open var hideBlock : (()-> Void)?

    open func hide(animated: Bool) {
        self.animated = animated
        queueHide()
    }

    open func show(animated: Bool) {
        self.animated = animated
        queueShow()
    }

    
    public init() {
        super.init(nibName: nil, bundle: nil)
        self.modalPresentationStyle = .overCurrentContext

    }
    
    required public init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    open override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        lxf_setupUI()
    }
    
    @objc open func btnClicked(){
        if backgroundHideEnabled {
            self.hide(animated: true)
            cancelBlock?()
        }
    }


    open func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
        return touch.view == self.view
    }

    open func lxf_setupUI() {
        
        self.view.backgroundColor = .clear
        self.view.addSubview(self.btn)
        btn.translatesAutoresizingMaskIntoConstraints = false
        btn.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 0).isActive = true  //顶部约束
        btn.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 0).isActive = true  //左端约束
        btn.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: 0).isActive = true  //右端约束
        btn.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: 0).isActive = true  //底部约束
        
        showView.isUserInteractionEnabled = true
        showView.addGestureRecognizer(dragGesture)
        self.view.addSubview(self.showView)
        
    }

    /// 拖动回调
    @objc open func onDrag(_ recognizer: UIPanGestureRecognizer) {
        if !showViewDragHideEnabled {
            return
        }
        let translation = recognizer.translation(in: recognizer.view)
        
        switch recognizer.state {
        case .changed:
            if translation.y >= 0 {
                self.showView.transform = CGAffineTransform(translationX: 0, y: translation.y)

            }
        case .ended:
            /// 若果小于一定距离，则退出
            if translation.y > (self.view.frame.height * 0.3) {
                hide(animated: true)
            }else {
                UIView.animate(withDuration: alertAnimationDuration) {
                    self.showView.transform = CGAffineTransform(translationX: 0, y: 0)
                }
            }
        default:
            break
        }
    }

}

extension LXAlertBaseViewController {
    
    open func alertHide() {
        if animated {
            UIView.animate(withDuration: alertAnimationDuration) {
                self.view.frame = CGRect(x: 0, y: UIScreen.main.bounds.height, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
                self.view.backgroundColor = .clear
            } completion: { (_) in
                self.view.removeFromSuperview()
                self.removeFromParent()
                self.hideBlock?()
            }
        }else{
            self.view.removeFromSuperview()
            self.removeFromParent()
            hideBlock?()
        }
    }

    open func alertShow() {
        guard let parentVC = parentVC ?? UIApplication.shared.rootViewController else {
            return
        }
        parentVC.addChild(self)
        if animated {
            self.view.backgroundColor = UIColor.clear
            self.view.frame = CGRect(x: 0, y: UIScreen.main.bounds.height, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
            parentVC.view.addSubview(self.view)
            UIView.animate(withDuration: alertAnimationDuration) {
                self.view.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
                self.view.backgroundColor = UIColor.init(white: 0, alpha: 0.6)
            }
        }else{
            parentVC.view.addSubview(self.view)
            self.view.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
            self.view.backgroundColor = UIColor.init(white: 0, alpha: 0.6)
        }
    }

}

